<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * date: 2021-12-01 09:56:57
 * @LastEditors: Sam
 * @LastEditTime: 2022-08-03 14:43:53
 */
namespace app\api\controller;
use app\__MODULE__\model\{Goods as GoodsModel,GoodsCart as CartModel,GoodsAttrValue,GoodsAttr};
use think\facade\Db;
use hg\apidoc\annotation as Apidoc;


/**
 * @Apidoc\Title("购物车")
 */

class GoodsCart extends ApiBase{


    /**
     * @Apidoc\Title("添加购物车")
     * @Apidoc\Url("/api/add_cart")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("goods_id", type="int",require=true, desc="商品id")
     * @Apidoc\Param("cart_num", type="int",require=true, desc="数量")
     * @Apidoc\Param("unique", type="string", desc="属性唯一值 多规格商品必填")
     * @Apidoc\Param("new", type="int",default="1",desc="是否直接购买 1 加入购物车直接购买 商品详情直接下订单使用  0 加入购物车" )
     * @Apidoc\Returned("cartId", type="int", desc="购物车id")
     */
	public function add(){
		$uid      = $this->get_user_id();
		$goods_id = request()->param('goods_id/d',0); //普通产品编号
		$cartNum  = request()->param('cart_num/d',1);//购物车数量
		$uniqueId = request()->param('unique',''); //属性唯一值
		$new      = request()->param('new/d',1); // 1 加入购物车直接购买  0 加入购物车
		 try{
		 	if (!$goods_id || !is_numeric($goods_id)) json_response(0,'参数错误');
		 	if (!$uniqueId) {
		 		$uniqueId = GoodsAttrValue::where(['product_id' => $goods_id])->value('unique');
		 	}
		 
		 	$res = CartModel::setCart($uid, $goods_id, $cartNum, $uniqueId, $new);
		 }catch(\Exception $e){
		 	json_response(0,$e->getMessage());
		 }
		 json_response(1,'添加成功',['cartId' => $res->id]);
	}

    /**
     * @Apidoc\Title("我的购物车")
     * @Apidoc\Url("/api/user_cart")
     * @Apidoc\Method("GET")
     * @Apidoc\Returned("cartId", type="int", desc="购物车id")
     */
	public function list(){
		  $list = CartModel::getUserProductCartList($this->get_user_id());
		  $temp = [];
		  if(count($list) && isset($list['valid'])){
		  	foreach($list['valid'] as $key=>$value){
		  		if($value['productInfo']['spec_type'] ==0){
		  			//单属性
		  			$temp[$key]['image'] = $value['productInfo']['image'];
		  			$temp[$key]['price'] = $value['productInfo']['price'];
		  		}else{
		  			$temp[$key]['image'] = $value['productInfo']['attrInfo']['image'];
		  			$temp[$key]['price'] = $value['productInfo']['attrInfo']['price'];
		  		}
		  			$temp[$key]['cart_num'] = $value['cart_num'];
		  			$temp[$key]['product_id'] = $value['product_id'];
		  			$temp[$key]['attr']  = $value['productInfo']['attrInfo']['suk'];
		  			$temp[$key]['cart_id'] = $value['id'];
		  	}
		  }
		  return json_response(1,'success',$list['valid']);
	}

    /**
     * @Apidoc\Title("增减购物车")
     * @Apidoc\Url("/api/cart_num")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("cart_id", type="int", desc="购物车id")
	 * @Apidoc\Param("number", type="int", desc="数量")
     */
	public function num(){
        $id  = request()->param('cart_id',0);
        $number = request()->param('number',0);
        if (!$id || !$number || !is_numeric($id) || !is_numeric($number)) json_response(0,'参数错误!');
        $res = CartModel::changeUserCartNum($id, $number, $this->get_user_id());
        if ($res)  json_response(1,'success');
        else json_response(0,GoodsCart::getErrorInfo('修改失败'));
	}

    /**
     * @Apidoc\Title("删除购物车")
     * @Apidoc\Url("/api/del_cart")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("cart_id", type="string", desc="购物车id 可传多个,号分割")
     */
    public function del()
    {
        $ids = request()->param('id/a',[]);
        if (!count($ids))
            json_response(0,'参数错误!');
        if(CartModel::removeUserCart($this->get_user_id(), $ids)){

            json_response(1,'success');
        }
        json_response(0,'清除失败！');
    }
}